package com.xpgk.mvc.btoShop;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bcost.SbPay;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.bentryRegister.CustomerProductSurplus;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productPrice.Price;
import com.xpgk.mvc.productSales.ProductSales;
import com.xpgk.mvc.productSales.ProductSalesOrder;
import com.xpgk.mvc.statistic.dailyTotalStatistic.DailyNursingTotal;
import com.xpgk.tools.ToolDateTime;

@Service(name = BoutShopService.serviceName)
public class BoutShopService extends BaseService {
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BoutShopService.class);
	public static final String serviceName = "boutShopService";
	
	/**保存顾客转出记录
	 * 
	 * @param bs
	 * @param organIds
	 * @param userIds
	 * @return
	 */
	public  String  saveOutShop(BtoShop  bs,List<CustomerProductSurplus>  cpsList,List<ProductSales> psList,String organIds,String userIds)
	{	
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd"); 
		String  date=time.format(new Date());
		
		BtoShop  bsop=BtoShop.dao.getOutShop(bs.getStr(BtoShop.column_customerIds), organIds);
		if(bsop!=null)
		{
			log.debug("该顾客已经转出店，请勿重复提交");
			throw new RuntimeException("该顾客已经转出店，请勿重复提交!");
		}else
		{
			if(bs!=null && bs.getStr(BtoShop.column_intoShopIds)!=null){//判断转入门店ids
				String inOrgIds = bs.getStr(BtoShop.column_intoShopIds);
				Organization org  = Organization.dao.findById(inOrgIds);
				if(org!=null && org.getInt(Organization.column_level)!=null && org.getInt(Organization.column_level)==4){
					//保存
					bs.set(BtoShop.column_outShopIds, organIds)
						.set(BtoShop.column_isConfirm, "0")
						.set(BtoShop.column_outTime, new Date())
						.set(BtoShop.column_outUserIds, userIds)
						.set(BtoShop.column_gxsj, new Date());
					if(!bs.save())
					{
						log.debug("保存顾客转出记录失败");
						throw new RuntimeException("保存顾客转出记录失败!");
					}
						
					
					//禁用顾客
					Customer  cs=Customer.dao.findById(bs.getStr(BtoShop.column_customerIds));
					cs.set(Customer.column_dataStatus, "5");
					if(!cs.update())
					{
						log.debug("禁用顾客失败");
						throw new RuntimeException("禁用顾客失败!");
					}else{
						String sql = "select * from sb_customer_detail where customerIds = ? and dataStatus in(1,2)";
						CustomerDetail customerDetail = CustomerDetail.dao.findFirst(sql,cs.getStr(Customer.column_ids));
						customerDetail.set(CustomerDetail.column_dataStatus, "5");
						if(!customerDetail.update())
						{
							log.debug("禁用顾客失败");
							throw new RuntimeException("禁用顾客失败!");
						}
					}
				}else{
					return "请选择转入门店";
				}
				
			}
			
		}
		
		
		//保存顾客产品剩余明细
		if(cpsList!=null && cpsList.size()>0)
		{
			Float price =0f;
			for(CustomerProductSurplus  cps:cpsList)
			{	
				CustomerProductSurplus  cusprosur=CustomerProductSurplus.dao.getCustomerProductSurplus(cps.getStr(CustomerProductSurplus.column_customerIds), cps.getStr(CustomerProductSurplus.column_productIds), organIds, "%"+date+"%");
				if(cusprosur!=null)
				{
					//更新
					cusprosur.set(CustomerProductSurplus.column_count, cps.getInt(CustomerProductSurplus.column_count))
							.set(CustomerProductSurplus.column_totalExperience, cps.getFloat(CustomerProductSurplus.column_totalExperience))
							.set(CustomerProductSurplus.column_proprice, cps.getFloat(CustomerProductSurplus.column_proprice))
							.set(CustomerProductSurplus.column_totalprice, cps.getFloat(CustomerProductSurplus.column_totalprice))
							.set(CustomerProductSurplus.column_gxsj,new  Date())
							.set(CustomerProductSurplus.column_jzjg,cps.getFloat(CustomerProductSurplus.column_jzjg));
					if(!cusprosur.update())
					{
						log.debug("更新顾客产品剩余明细失败");
						throw new RuntimeException("更新顾客产品剩余明细失败!");
					}
						
						
						
					
					
				}else
				{	
				
					String cusproSql=getSqlMy(CustomerProduct.sqlId_selByCusAndPro);
					CustomerProduct  cp=CustomerProduct.dao.findFirst(cusproSql,cps.getStr(CustomerProductSurplus.column_customerIds),cps.getStr(CustomerProductSurplus.column_productIds),organIds);
					
					if(cp!=null)
					{
						cps.set(CustomerProductSurplus.column_proExperience, cp.getFloat(CustomerProduct.column_proExperience));
						cps.set(CustomerProduct.column_totalExperience, cp.getFloat(CustomerProduct.column_totalExperience));
					}
					cps.set(CustomerProductSurplus.column_organIds, organIds)
					.set(CustomerProductSurplus.column_gxsj, new Date());
					if(!cps.save())
					{
						log.debug("顾客产品剩余明细保存失败");
						throw new RuntimeException("顾客产品剩余明细保存失败!");
					}
					
				}
				
			}
			//转出店保存费用支出记录
			
			ProductSalesOrder productSalesOrder = new ProductSalesOrder();
			for(ProductSales  pss:psList){
				price  = price+pss.getFloat(ProductSales.column_price);
				
			}
			SbPay sbPay = new SbPay();
			sbPay.set(SbPay.column_organizationIds, organIds)
			.set(SbPay.column_price, price)
			.set(SbPay.column_exUserIds, userIds)
			.set(SbPay.column_userIds, userIds)
			.set(SbPay.column_bz, "顾客转店")
			.set(SbPay.column_createId, userIds)
			.set(SbPay.column_date, new Date())
			.set(SbPay.column_type,"eb8c58b1af13427f98c003ff3aba61ed")
			.set(SbPay.column_createDate, new Date());
			if(!sbPay.save()){
				log.debug("费用支出保存失败");
				throw new RuntimeException("费用支出保存失败!");
			}
			
			//保存日销售综合表
			DailyNursingTotal  dnt = DailyNursingTotal.dao.getDnt(organIds,new Date());
			
			
			
			if(dnt != null)
			{
				dnt.set(DailyNursingTotal.column_zc, dnt.getDouble(DailyNursingTotal.column_zc)+(double)price);
				if (!dnt.update()) {
					log.debug("更新日综合查询表失败");
					throw new RuntimeException("更新日综合查询表失败!");
				}
			}
			else
			{	
				
				DailyNursingTotal  dntNew = new DailyNursingTotal();
				dntNew.set(DailyNursingTotal.column_organids, organIds)
					  .set(DailyNursingTotal.column_date, ToolDateTime.format(new Date(), ToolDateTime.pattern_ymd));
				dntNew.set(DailyNursingTotal.column_zc,(double)price);
				if (!dntNew.save()) {
					log.debug("更新日综合查询表失败");
					throw new RuntimeException("更新日综合查询表失败!");
				}
			}
		}
		
		return ConstantRender.render_success_code;
	}
	
}
